package com.regis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regis.comment.uilts.ThreadDataUtil;
import com.regis.entity.db.OrderDetail;
import com.regis.entity.db.Orders;
import com.regis.entity.db.ShoppingCart;
import com.regis.entity.vo.ResultVo;
import com.regis.service.OrderDetailService;
import com.regis.service.OrdersService;
import com.regis.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 计算机系 ITAEM 摆渡人
 * @description 订单模块
 * @date 2022/8/24 2:21
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 用户下单
     * @param orders Orders
     * @return ResultVo<String>
     */
    @CacheEvict(value = "order",allEntries = true)
    @PostMapping("/submit")
    public ResultVo<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return ResultVo.success("下单成功");
    }

    /**
     * 用户获取订单信息
     * @param page page
     * @param pageSize pageSize
     * @return @GetMapping("/userPage")
     */
    @Cacheable(value = "order",key = "#page + '_' + #pageSize",unless = "#result.data.records == null")
    @GetMapping("/userPage")
    public ResultVo<Page<Orders>> userPage(Integer page,Integer pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, ThreadDataUtil.getValue());
        ordersService.page(pageInfo,queryWrapper);
        return ResultVo.success(pageInfo);
    }

    /**
     * 员工获取订单信息
     * @param page page
     * @param pageSize pageSize
     * @return ResultVo<Page<Orders>>
     */
    @Cacheable(value = "order",key = "#page + '_' + #pageSize",
            condition = "#number == null && #beginTime == null && #endTime == null",
            unless = "#result.data.records == null")
    @GetMapping("/page")
    public ResultVo<Page<Orders>> page(Integer page, Integer pageSize, String number, String beginTime,
                                       String endTime){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(number),Orders::getNumber,number);
        if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(beginTime, formatter);
            LocalDateTime end = LocalDateTime.parse(endTime, formatter);
            queryWrapper.between(Orders::getOrderTime,start,end);
        }
        ordersService.page(pageInfo,queryWrapper);
        List<Orders> records = pageInfo.getRecords();

        return ResultVo.success(pageInfo);
    }

    /**
     * 员工修改订单状态
     * @param orders orders
     * @return ResultVo<String>
     */
    @CacheEvict(value = "order",allEntries = true)
    @PutMapping
    public ResultVo<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return ResultVo.success("更改成功");
    }

    /**
     * 用户再来一单
     * @param orders Orders
     * @return String
     */
    @PostMapping("/again")
    public ResultVo<String> again(@RequestBody Orders orders){
        List<OrderDetail> list = orderDetailService.list(new LambdaQueryWrapper<OrderDetail>().eq(orders.getId() != null,
                OrderDetail::getOrderId, orders.getId()));
        List<ShoppingCart> shoppingCarts = list.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item,shoppingCart);
            shoppingCart.setId(null);
            shoppingCart.setUserId(ThreadDataUtil.getValue());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCarts);
        return ResultVo.success("添加成功");
    }
}
